//
//  StatusCellTopView.swift
//  新浪微博
//
//  Created by 赵春浩 on 15/12/19.
//  Copyright © 2015年 DreamFlyingCow. All rights reserved.
//

import UIKit

class StatusCellTopView: UIView {

    // 定义微博模型属性
    var status: Status? {
        didSet {
            // 在这里绑定数据
            iconView.sd_setImageWithURL(status?.user?.headURL)
            nameLabel.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrankImage
            verified_type_image.image = status?.user?.verified_type_image
            
            contentLabel.text = status?.text
            
            // 设置配图视图的, 图片的数组, 数据源
            pictureView.imageURLs = status?.imageURLs
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置背景颜色  调试用的
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 自定义顶部视图
    private func setupUI() {
        
        // 添加子视图 
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(verified_type_image)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            
            make.size.equalTo(CGSize(width: StatusCellHeadImageWidth, height: StatusCellHeadImageWidth))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // 对顶部视图的底部设置约束
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
        }
        
    }
    
    // 懒加载所有的子视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(title: "哈哈", color:  themeColor, fontSize: 14)
    private lazy var mbrankImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verified_type_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var timeLabel: UILabel = UILabel(title: "12:12", color: themeColor, fontSize: 10)
    private lazy var sourceLabel: UILabel = UILabel(title: "逗比", color:  themeColor, fontSize: 10)
    private lazy var contentLabel: UILabel = UILabel(title: "真的是日了狗了真的是日了狗了真的是日了狗了真的是日了狗了真的是日了狗了", color:  UIColor.darkGrayColor(), fontSize: 14, margin:StatusCellMargin)
    
    
    // 配图视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
}
